public class MyTest {
    class ListNode{
        int val;
        ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }
    public ListNode partition(ListNode pHead, int x) {
        // write code here
        if(pHead==null){
            return null;

        }
        ListNode as = null;
        ListNode ae = null;
        ListNode bs = null;
        ListNode be = null;
        ListNode cur = pHead;

        while(cur!=null){
            if(cur.val < x){
                if(as == null){
                    as = cur;
                    ae = cur;
                    //cur = cur.next;
                }else{
                    ae.next = cur;
                    ae = ae.next;
                    //cur = cur.next;
                }
            }else{
                if(bs == null){
                    bs = cur;
                    be = cur;
                    //cur = cur.next;
                }else{
                    be.next = cur;
                    be = be.next;
                    //cur = cur.next;
                }
            }
            cur = cur.next;
        }
        if( as == null){
            return bs;
        }
        ae.next = bs;
        if(bs!=null){
            be.next = null;
        }
        return as;
    }
}
